# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
### BEGIN LICENSE
# Copyright (C) YYYY Alexandros Fotiadis fotiadis90@gmail.com
# This program is free software: you can redistribute it and/or modify it 
# under the terms of the GNU General Public License version 3, as published 
# by the Free Software Foundation.
# 
# This program is distributed in the hope that it will be useful, but 
# WITHOUT ANY WARRANTY; without even the implied warranties of 
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
# PURPOSE.  See the GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along 
# with this program.  If not, see <http://www.gnu.org/licenses/>.
### END LICENSE

import optparse,threading

from locale import gettext as _

from gi.repository import Gtk # pylint: disable=E0611

from gi.repository import GObject

from android_keyboard import AndroidKeyboardWindow

from android_keyboard_lib import set_up_logging, get_version



class DThread(threading.Thread) :
	
	from android_keyboard_lib.xlikan_lib import log; logger = log()
	
	def __init__(self,CT,window):
		self.logger.debug("Initializing Device Thread")
        threading.Thread.__init__(self)
        self.CheckTime = CT  #Init The Recheck Time 
        self.quit = False
        self.win = window
        
    def run(self):
		import time
		from android_keyboard_lib.FuncLib import readDevices,ConnectCurrentDevice
		
        while not self.quit:
			self.logger.debug("Check Devices (Multithread)")
			
			readDevices(self.win)
			
			ConnectCurrentDevice(self.win)
			time.sleep(self.CheckTime)
		
		self.logger.debug("Gtk Terminated, Thread will Finisht")


def parse_options():
    """Support for command line options"""
    parser = optparse.OptionParser(version="%%prog %s" % get_version())
    parser.add_option(
        "-v", "--verbose", action="count", dest="verbose",
        help=_("Show debug messages (-vv debugs android_keyboard_lib also)"))
    (options, args) = parser.parse_args()

    set_up_logging(options)

def main():
    ##Extra Command Register
    parse_options()
    
    #Run the application.    
    window = AndroidKeyboardWindow.AndroidKeyboardWindow()
    
    window.show()
    
	##Start Device Thread
    GObject.threads_init() #GObject Helps to give Access on Thread to stay alive/running 
    
	DeviceThread = DThread(2,window) #Initializing 
	
    DeviceThread.start() #Start Thread
    
    
    Gtk.main() 
   
	##Terminate Thread
	
    DeviceThread.quit = True #Stop Thread !This Command will running when the GTK.main are Finisht or Terminated

	
    

